<?php
namespace Home\Controller;
use Think\Controller;

class LoginController extends Controller{

	public function index(){
		$this->display();
	}

	public function handle(){
		$db=M('admin');
		$user=$db->where(array('loginname'=>$_POST['name']))->find();
		$salt=$user['salt'];
		$password=$_POST['password'];
		$a=$salt.''.$password;
		// md5加密后进行比对
		if(!$user||$user['password']!=md5($a)){
 			$this->error('账号或密码错误');
		}
		// 把admin_id和is_superadmin存在session中
		$_SESSION['is_superadmin'] = $user['is_superadmin'];
		$_SESSION['admin_id'] = $user['id'];
		if($_SESSION['is_superadmin'] == 1){
			$this->redirect('Superadmin/index');
		}else{
			$this->redirect('Index/index');
		}
		
	}

	public function logout(){
		unset($_SESSION['admin_id']);
		$this->redirect('Index/index');
	}

}

